package com.dxl.lanju.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.dao.ProductDao;
import com.dxl.service.common.BaseService;
import com.lanju.bean.ProductSkuBean;
import com.lanju.entity.Product;
import com.lanju.entity.ProductSku;

@Service
@Transactional
public class ProductService extends BaseService {
	private static final Logger LOG = Logger.getLogger(ProductService.class);
	@Autowired
	private ProductDao productDao;
	
	public RespData save(Product product) {
		Product srcProcuct;
		boolean isInsert = false;
		if (Utils.isNotBlank(product.getId())) {
			srcProcuct = this.get(Product.class, product.getId());
			srcProcuct.setName(product.getName());
			srcProcuct.setDescription(product.getDescription());
			srcProcuct.setPath(product.getPath());
			srcProcuct.setPathSl(product.getPathSl());
			srcProcuct.setCateId(product.getCateId());
			srcProcuct.setState(product.getState());
		} else {
			if (this.productDao.exist(product.getName())) {
				return RespData.instance(RespData.FAIL, "产品已经存在！");
			}
			srcProcuct = product;
			isInsert = true;
		}
		super.save(srcProcuct);
		if (isInsert) {
			ProductSku defaultSku = new ProductSku();
			defaultSku.setProdId(srcProcuct.getId());
			defaultSku.setCateId(product.getCateId());
			super.save(defaultSku);
		}
		Map<String, Object> params = new HashMap<String, Object>(), values = new HashMap<String, Object>();
		values.put("state", srcProcuct.getState());
		params.put("prodId", srcProcuct.getId());
		this.update(ProductSku.class, params, values);
		return RespData.instance(RespData.SUCCESS, srcProcuct.getId());
	}
	
	public RespData del(String[] ids) {
		if (Utils.isEmpty(ids)) {
			return RespData.instance(RespData.SUCCESS);
		} else {
			try {
				this.del(Product.class, ids);
				Map<String, Object> params = new HashMap<String, Object>();
				for (String id : ids) {
					params.put("prodId", id);
					this.del(ProductSku.class, params);
				}
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	public RespData state(String[] ids, Integer state) {
		if (Utils.isEmpty(ids)) {
			return RespData.instance(RespData.SUCCESS);
		} else {
			try {
				Map<String, Object> params = new HashMap<String, Object>(), values = new HashMap<String, Object>();
				values.put("state", state);
				this.update(Product.class, ids, values);
				for (String id : ids) {
					params.put("prodId", id);
					this.update(ProductSku.class, params, values);
				}
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	public Page<Product> list(String cateId, String keyword, Integer state, Integer page, Integer rows) {
		return productDao.list(cateId, keyword, state, page, rows);
	}
	
	public Page<ProductSkuBean> listProductSku(String cateId, String keyword, Integer state, Integer pageNo, Integer pageSize) {
		return productDao.listProductSku(cateId, keyword, state, pageNo, pageSize);
	}
	
	public List<ProductSku> listSku(String prodId) {
		return productDao.listSku(prodId);
	}
	
	public RespData saveSku(ProductSku productSku) {
		ProductSku srcProcuctSku;
		if (Utils.isNotBlank(productSku.getId())) {
			srcProcuctSku = this.get(ProductSku.class, productSku.getId());
			srcProcuctSku.setColor(productSku.getColor());
			srcProcuctSku.setProdId(productSku.getProdId());
			srcProcuctSku.setTaste(productSku.getTaste());
			srcProcuctSku.setUnit(productSku.getUnit());
			if (Utils.isNotBlank(productSku.getBigUnit()) && Utils.isNotNull(productSku.getUnitTimes())) {
				srcProcuctSku.setBigUnit(productSku.getBigUnit());
				srcProcuctSku.setUnitTimes(productSku.getUnitTimes());
				JSONObject jo = new JSONObject();
				jo.put("unit", productSku.getBigUnit());
				jo.put("decimal", productSku.getUnitTimes());
				srcProcuctSku.setViewUnit(jo.toJSONString());
			} else {
				srcProcuctSku.setViewUnit(null);
				srcProcuctSku.setBigUnit(null);
				srcProcuctSku.setUnitTimes(null);
			}
			srcProcuctSku.setUnitPrice(productSku.getUnitPrice());
			srcProcuctSku.setState(productSku.getState());
			srcProcuctSku.setPath(productSku.getPath());
		} else {
			srcProcuctSku = productSku;
		}
		Product prod = this.get(Product.class, productSku.getProdId());
		if (Utils.isNotNull(prod)) {
			srcProcuctSku.setCateId(prod.getCateId());
		}
		super.save(srcProcuctSku);
		return RespData.instance(RespData.SUCCESS, srcProcuctSku.getId());
	}
	
	public RespData delSku(String id) {
		if (Utils.isBlank(id)) {
			return RespData.instance(RespData.SUCCESS);
		} else {
			try {
				this.del(ProductSku.class, id);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
}
